package com.kushe.dao;

import com.kushe.config.Config;
import com.kushe.entity.Order;
import com.kushe.util.core.DaoFactory;
import com.kushe.util.core.DataAccess;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;

import java.util.List;

/**
 * Created by WhiteBlue on 15/5/2.
 */
public class OrderDao extends DataAccess<Order> {

//    private final static SimpleDateFormat TIME_ORDER_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssffff");


    @Override
    public Order get(int id) {
        return null;
    }


    public Order getForMe(int design_id, int user_id) {
        return getDao().fetch(Order.class, Cnd.where("design_id", "=", design_id).and("user_id", "=", user_id));
    }


    /**
     * 得到用户信息上显示的四个订单
     *
     * @param user_id 用户id
     */
    public List<Order> getForUserLimit(int user_id) {
        return getDao().query(Order.class, Cnd.where("user_id", "=", user_id).limit(4));
    }


    /**
     * 得到用户的所有订单
     *
     * @param user_id 用户id
     */
    public QueryResult myOrder(int user_id, int pageNumber) {
        Pager pager = getDao().createPager(pageNumber, Config.PAGE_SIZE);
        List<Order> list = getDao().query(Order.class, Cnd.where("user_id", "=", user_id).desc("time"), pager);
        pager.setRecordCount(getDao().count(Order.class, Cnd.where("user_id", "=", user_id)));
        return new QueryResult(list, pager);
    }

    public List<Order> myOrderLimit(int user_id) {
        return getDao().query(Order.class, Cnd.where("user_id", "=", user_id).limit(8).desc("time"));
    }

    public boolean saveOrder(Order order) {
        try {
            getDao().insert(order);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<Order> getForDesign(int design_id) {
        return getDao().query(Order.class, Cnd.where("design_id", "=", design_id).limit(5).desc("time"));
    }


//    /**
//     * 生成订单唯一ID
//     *
//     * @param user_id 用户的ID
//     */
//    private String makeCodeId(int user_id) {
//        Random random = new Random();
//
//        String strRandom = (random.nextInt(10) + "000").substring(0, 3);
//
//        return user_id + TIME_ORDER_FORMAT.format(new Date()) + strRandom;
//    }


}
